package com.yanqu.road.server.manager.activity.jinli;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.server.CrossBaseServer;
import com.yanqu.road.server.TempCrossMgr;
import com.yanqu.road.utils.date.DateHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 锦鲤签
 */
public class CrossJinLiMgr extends TempCrossMgr {

    public final static Map<Integer, CrossJinLiActivity> activityMap = new ConcurrentHashMap<>();

    public static Map<Integer, CrossJinLiActivity> getActivityMap() {
        return activityMap;
    }

    public static synchronized CrossJinLiActivity getActivity(int activityId) {
        if (!activityMap.containsKey(activityId)) {
            // 读配置 获取活动信息 保证有活动信息
            ActivityInfo activityInfo = ActivityBussiness.getActivityInfoByActivityId(activityId);
            if (activityInfo == null) {
                return null;
            }
            // 不在展示期也不处理了
            if (!ActivityHelper.activityInShowTime(activityInfo)) {
                return null;
            }
            CrossJinLiActivity activity = new CrossJinLiActivity(activityInfo);
            activityMap.put(activityId, activity);
        }
        return activityMap.get(activityId);
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        List<Integer> activityTypeList = new ArrayList<>();
        activityTypeList.add(eActivityType.JinLi.getValue());
        Map<Integer, ActivityInfo> activityInfoMap = ActivityBussiness.getOpenCrossRankActivityInfoMapByCrossServerId(activityTypeList, DateHelper.getCurrentDateTimeString(), CrossBaseServer.getInstance().getServerId());
        // 重新加载数据
        for (ActivityInfo activityInfo : activityInfoMap.values()) {
            reloadActivity(activityInfo);
        }
        return true;
    }

    public static void reloadActivity(ActivityInfo activityInfo) {
        if (activityInfo == null) {
            return;
        }
        int activityId = activityInfo.getActivityId();
        CrossJinLiActivity tempActivity = new CrossJinLiActivity(activityInfo);
        activityMap.put(activityId, tempActivity);
    }

    @Override
    public boolean stop() {
        return true;
    }

    @Override
    public boolean save() {
        if (activityMap.size() > 0) {
            getLogger().info("jinli save start");
        }
        for (CrossJinLiActivity activity : activityMap.values()) {
            activity.save();
        }
        if (activityMap.size() > 0) {
            getLogger().info("jinli save end");
        }
        return true;
    }

    @Override
    public boolean removeExpireData() {
        List<Integer> removeList = new ArrayList<>();
        long nowTime = System.currentTimeMillis();
        for (int activityId : activityMap.keySet()) {
            CrossJinLiActivity activity = activityMap.get(activityId);
            if (activity.getActivityInfo() == null || (nowTime > activity.getActivityInfo().getEndShowTime() * 1000 + 6 * 3600 * 1000)) {
                removeList.add(activityId);
            }
        }
        for (int activityId : removeList) {
            activityMap.remove(activityId);
            getLogger().debug("remove activity {}", activityId);
        }
        return true;
    }
}
